/*
 * Canvas Game Library
 * Module: Tile
 * 
 * Copyright Chainsawrus Wreaks 2011
 * All rights reserved.
 */

/**
 * @fileOverview A module that adds basic tilemap capabilities to CGL, will have import/export functions when
 * used with the AJAX module!
 */

/**
 * Tile module namespace
 * @namespace CGL.tile
 */

CGL.tile = {
	Tile : function() {
		var tile = {
			id : 0,
			data : null,
			
			init : function(uid) {
				this.id = uid;
			},
			
			setId : function(srcdata) {
				this.id = srcdata;
			},
			
			getId : function() {
				return tile.id;
			},
			
			setData : function(data) {
				this.data = data;
			},
			
			getData : function() {
				return this.data;
			}
		};
		return tile;
	},

	TileMap : function(c, r, tWidth, tHeight) {
		var tilemap = {
			cols : c,
			rows : r,
			width : tWidth,
			height : tHeight,
			tiles : new Array(c),
			
			init : function() {
				for(var i = 0; i < this.tiles.length; i++)
				{
					this.tiles[i] = new Array(this.rows);
				}
				
				for(var i = 0; i < this.tiles.length; i++)
				{
					for(var j = 0; j < this.tiles[i].length; j++)
					{
						this.tiles[i][j] = new CGL.tile.Tile();
						this.tiles[i][j].setId(0);
					}
				}
			},
		
			setTileId : function(tilex, tiley, id) {
				this.tiles[tilex][tiley].setId(id);
			},
			
			getTileId : function(tilex, tiley) {
				return this.tiles[tilex][tiley].getId();
			},
			
			setTileData : function(tilex, tiley, data) {
				this.tiles[tilex][tiley].setData(data);
			},
			
			getTileData : function(tilex, tiley) {
				return this.tiles[tilex][tiley].getData();
			},
			
			getWidth : function() {
				return this.width;
			},
			
			getHeight : function() {
				return this.height;
			},
			
			getRows : function() {
				return this.rows;
			},
			
			getColumns : function() {
				return this.cols;
			}
		};
		return tilemap;
	},
	
	IsometricMap : function(c, r, tWidth, tHeight) {
		isomap = {
			isometricMap : new CGL.tile.TileMap(c, r, tWidth, tHeight),
			x : 0,
			y : 0,
			images : new Array(),
			
			init : function() {
				var tempRect, width, height;
				
				width = this.isometricMap.getWidth();
				height = this.isometricMap.getHeight();
				
				this.isometricMap.init();
				
				for(var i = 0; i < this.isometricMap.getColumns(); i++)
				{
					for(var j = 0; j < this.isometricMap.getRows(); j++)
					{
						tempRhomb = new CGL.collision.geom.Rhomb((i + j) * width/2, (j - i) * -(height/2), width, height);
						this.isometricMap.setTileData(i, j, tempRhomb);
					}
				}
				CGL.util.printLn("Map Loaded!");
			},
			
			getTile : function(x, y) {
				for(var i = 0; i < this.isometricMap.getColumns(); i++)
				{
					for(var j = 0; j < this.isometricMap.getRows(); j++)
					{
						tempRhomb = this.isometricMap.getTileData(i, j);
						if(CGL.collision.detect.PointRhomb(new CGL.collision.geom.Point(x, y), tempRhomb))
						{
							return new CGL.collision.geom.Point(i, j);
						}
					}
				}
			},
			
			render : function(ctx) {
				var width, height;
				
				width = this.isometricMap.getWidth() / 2;
				height = this.isometricMap.getHeight() / 2;
				for(var i = 0; i < this.isometricMap.getColumns(); i++)
				{
					for(var j = this.isometricMap.getRows()-1; j >= 0; j--)
					{
						this.images[this.isometricMap.getTileId(i, j)].render(ctx, this.x + (i + j) * width, this.y + (j - i) * -(height));						
					}
				}
				/*
				for(var i = 0; i < this.isometricMap.getColumns(); i++)
				{
					for(var j = 0; j < this.isometricMap.getRows(); j++)
					{
						rhomb = this.isometricMap.getTileData(i, j);
						rhomb.draw(ctx);
						ctx.fillText(i + " : " + j, this.x + (i + j) * width/2 + 20, this.y + (j - i) * -(height/2) + 20);
					}
				}
				*/
			},
			
			renderBlock : function(ctx, i, j) {
				var width, height;
				i++;
				width = this.isometricMap.getWidth();
				height = this.isometricMap.getHeight();
				ctx.fillStyle = "red";
				ctx.fillRect((this.x + (i + j) * width/2) - 5, (this.y + (j - i) * -(height/2)) - 5, 10, 10);
			},
			
			setPos : function(x, y) {
				this.x = x;
				this.y = y;
				
				for(var i = 0; i < this.isometricMap.getColumns(); i++)
				{
					for(var j = 0; j < this.isometricMap.getRows(); j++)
					{
						rhomb = this.isometricMap.getTileData(i, j);
						rhomb.transform(x, y);
						this.isometricMap.setTileData(i, j, rhomb);
					}
				}
			},
			
			setTileId : function(x, y, id) {
				this.isometricMap.setTileId(x, y, id);
			},
			
			getTileId : function(x, y) {
				return this.isometricMap.getTileId(x, y);
			},
			
			getTileData : function(x, y) {
				return this.isometricMap.getTileData(x, y);
			},
			
			setTileData : function(x, y, data) {
				this.isometricMap.setTileData(x, y, data);
			},
			
			addSprite : function(image) {
				this.images.push(image);
			},
			
			getColumns : function() {
				return this.isometricMap.getColumns();
			},
			
			getRows : function() {
				return this.isometricMap.getRows();
			}
		};
		return isomap;
	}
};